<?php
class Ticketmodel extends Model {
	function Ticketmodel() {
		parent::Model();
	}
	function show_id() {
		$this->db->select('ticket_id');
		$this->db->from('ticket');
		$this->db->order_by('ticket_id','desc');
		$this->db->limit('1');
		
		return $this->db->get();
	}
	function show_reference($user_id) {
		$this->db->select('*');
		$this->db->from('ticket');
		$this->db->where('user_id',$user_id);
		$this->db->order_by('ticket_id','desc');
		$this->db->limit('1');
		
		return $this->db->get();
	}
	function show_user_id($name)  {
		$this->db->select('user_id,name');
		$this->db->from('user');
		$this->db->where('user_id',$name);
		
		return $this->db->get();
	}
	function show_product() {
		$this->db->select('*');
		$this->db->from('product');
		$this->db->order_by('prod_id','desc');
		return $this->db->get();
	}
	
	function ticket_insert($ticket_insert) {
		$this->db->insert('ticket',$ticket_insert);
	}
	
	function search_reference($id,$offset) {
		$this->db->select('a.ticket_id,a.title,a.user_id,a.open_datetime,a.target_date,a.close_date,a.status,
		a.level_o_impact,a.prob_o_impact,a.severity_level,a.prod_id,b.name,c.product,a.images1,a.images2,a.images3,a.images4,a.images5,a.images6,a.images7,a.images8,a.images9,a.images10');
		$this->db->from('ticket a,user b,product c');
		$this->db->where('a.user_id = b.user_id and a.prod_id = c.prod_id');
		$this->db->where('a.user_id',$id);
		$this->db->order_by('a.ticket_id','desc');
		$this->db->limit(10,$offset);

		
		return $this->db->get();
	}
	function show_id_comments() {
		$this->db->select('*');
		$this->db->from('comments');
		$this->db->order_by('comment_id','desc');
		$this->db->limit('1');
		
		return $this->db->get();
	}
	
	function insert_comments($insert_coments) {
		$this->db->insert('comments',$insert_coments);
	}
	
	function ubah_date($id) {
		$this->db->select('*');
		$this->db->from('comments');
		$this->db->where('ticked_id',$id);
		
		return $this->db->get();
	}
/* Manage Ticket 
----------------------------------------------------------------------------------------*/
	function count_all_num_rows() {
		return $this->db->count_all('ticket');
	}
	//tampilkan 10 baris berita terkini di urutkan berdasarkan id beria
	function get_last_ten_berita($offset) {
		$this->db->select('a.ticket_id,a.title,a.user_id,a.open_datetime,a.target_date,a.close_date,a.status,
		a.level_o_impact,a.prob_o_impact,a.severity_level,a.prod_id,b.name,c.product,a.images1,a.images2,a.images3,a.images4,a.images5,a.images6,a.images7,a.images8,a.images9,a.images10');
		$this->db->from('ticket a,user b,product c');
		$this->db->where('a.user_id = b.user_id and a.prod_id = c.prod_id ');
		
		
		$this->db->order_by('ticket_id','desc');
		$this->db->limit(20,$offset);
		return $this->db->get();
	}
	function close($offset) {
		$this->db->select('a.ticket_id,a.title,a.user_id,a.open_datetime,a.target_date,a.close_date,a.status,
		a.level_o_impact,a.prob_o_impact,a.severity_level,a.prod_id,b.name,c.product,a.images1,a.images2,a.images3,a.images4,a.images5,a.images6,a.images7,a.images8,a.images9,a.images10');
		$this->db->from('ticket a,user b,product c');
		$this->db->where('a.user_id = b.user_id and a.prod_id = c.prod_id');
		$this->db->where('a.status','Aktif');
		
		$this->db->order_by('ticket_id','desc');
		$this->db->limit(20,$offset);
		return $this->db->get();
	}
	
	function delete($id_ticket) {
		$this->db->where('ticket_id',$id_ticket);
		$this->db->delete('ticket');
	}
	
	function update_manage($id_update) {
		$this->db->select('a.*,b.name,c.product');
		$this->db->from('ticket a, user b,product c');
		$this->db->where('a.user_id = b.user_id and a.prod_id = c.prod_id');
		$this->db->where('ticket_id',$id_update);
		
		return $this->db->get();
		
	}
	
	function coments2($id_show) {
		$this->db->select('*');
		$this->db->from('comments');
		$this->db->where('ticked_id',$id_show);
		
		return $this->db->get();
	}
	
	function update_ticket_process($id2,$update_c) {
		$this->db->where('ticket_id',$id2);
		$this->db->update('ticket',$update_c);
	}
	
	function update_comments($a,$insert_coments) {
		$this->db->where('comment_id',$a);
		$this->db->update('comments',$insert_coments);
	}
	function insert_update_comments($insert_coments) {
		$this->db->insert('comments',$insert_coments);
	}
	
	// cek validation coments ............
	function cek_update_comments($a) {
		$this->db->select('*');
		$this->db->where('comment_id',$a);
		
		return $this->db->get('comments');
	}
	
	/*function update_of_critical() {
		$this->db->where('DATEDIFF(CURDATE(),open_datetime) > 1');
		$this->db->where('severity_level="Critical"');
		$this->db->set('status ="Aktif"')
	}*/
/* comments2
--------------------------------------------------------------------------------*/
	function addComent2($idcmnt) {
		$this->db->insert('comments2',$idcmnt);
		
	}
	function show_id_coments2() {
		$this->db->select('*');
		$this->db->from('comments2');
		$this->db->order_by('id','desc');
		$this->db->limit('1');
		
		return $this->db->get();
	}
	
	function show_id_coments3() {
		$this->db->select('*');
		$this->db->from('comments');
		$this->db->order_by('id','desc');
		$this->db->limit('1');
		
		return $this->db->get();
	}
	
	
	function inset_cmmnt3($insert_c) {
		$this->db->insert('comments',$insert_c);
	}
	
	
	function show_idcmnt_by_ticket($ac ) {
		$this->db->select('a.*,b.*');
		$this->db->from('comments2 a,user b');
		$this->db->where('a.user_id = b.user_id');
		$this->db->where('ticket_id',$ac );
		$this->db->order_by('id','asc');
		
		return $this->db->get();
	}
/* end Comments2
-------------------------------------------------------------------------------*/

/* CLOSE TICKET
-------------------------------------------------------------------------------*/
	function close_ticket($a,$b) {
		$this->db->where('ticket_id',$a);
		$this->db->update('ticket',$b);
	}
/* END CLOSE
--------------------------------------------------------------------------------*/

	function count_all($id) {
		$sql = "SELECT a.*,b.*,c.* FROM ticket a,user b,product c where a.user_id = b.user_id and a.prod_id = c.prod_id and a.user_id='$id' ";
          $q = $this->db->query($sql);
          return $q->num_rows();
	}
	
/* IMAGES
-------------------------------------------------------------------------------*/
	function images_id() {
		$this->db->select('*');
		$this->db->from('images');
		$this->db->order_by('image_id','desc');
		$this->db->limit('1');
		
		return $this->db->get();
	}
	
	

/* END IMAGES
-------------------------------------------------------------------------------*/

/* UPDATE COMMENTS
------------------------------------------------------------------------------------*/
	function update_comment($idcmnt) {
		$this->db->select("a.*,b.name");
		$this->db->from("comments2 a,user b");
		$this->db->where("a.user_id = b.user_id");
		$this->db->where('id',$idcmnt);
		
		return $this->db->get();
	}
	
	function update_comments2_all($a1,$b1) {
		$this->db->where('id',$a1);
		$this->db->update('comments2',$b1);
	}

/* END UPDATE COMMENTS
------------------------------------------------------------------------------------*/

/* Donwload Ticket */
	function download_ticket($idticket) {
		$this->db->select('a.*,b.product,c.name,d.*');
		$this->db->from('ticket a,product b,user c,dept d');
		$this->db->where('a.prod_id = b.prod_id and a.user_id = c.user_id and d.prod_id = b.prod_id');
		$this->db->where('ticket_id',$idticket);
		
		
		return $this->db->get();
	}
/* end download Ticket */
}
?>